<?php

namespace App\Services\Repositories\Admin;

use App\Models\Admin\CustomerAdmin;
use App\Models\Customer\Customer;
use App\Services\Repositories\Connector\AdminInterface;
use App\Services\Utils\ApiChecker;
use Tymon\JWTAuth\Contracts\JWTSubject;


class CustomerAdminRepository implements AdminInterface
{
    use ApiChecker;

    /**
     * @param string $account
     * @return CustomerAdmin
     * @throws \Exception
     */
    public function findAdmin(string $account): CustomerAdmin
    {
        /** @var CustomerAdmin|null $admin */
        $admin = (new CustomerAdmin)
            ->where('account', $account)
            ->with('customer.categories')
            ->first();

        $this->checkIsTrue($admin, trans('middleware.account_not_found'));
        $this->checkUnless($admin->is_deleted, trans('middleware.account_disabled'));
        $this->checkUnless($admin->customer->is_deleted, trans('middleware.customer_admin.customer_disabled'));

        return $admin;
    }

    /**
     * @param JWTSubject $admin
     * @return CustomerAdmin|JWTSubject
     */
    public function loginAdminInfo(JWTSubject $admin)
    {
        /** @var CustomerAdmin $admin */
        $admin->provider_type = $admin->customer->provider_type ?? null;
        $admin->has_hair = $admin->customer->has_hair ?? null;
        $admin->has_youzan = $admin->customer->has_youzan ?? null;
        $admin->has_youzan_authorize = $admin->customer->youzan_authorizer_id ?? null;
        $admin->store_id = null;

        // 客户是否显示公司管理模块
        $admin->has_company_manage_module = ($admin->customer_id === Customer::ID_SUN_HUNG_KAI) ? true : false;

        return $admin;
    }
}